import NewWebSocket from '../libs/utils/websocket'
import { ref, onUnmounted } from 'vue'
const PushSocket = () => {
  // 重新开启socket
  const startSocket = () => {
    if (socket.socket) return
    socket.init()
  }
  // 暂停socket
  const closeSocket = () => {
    socket.close()
  }
  // 发送信息给后端
  const socketSend = (data) => {
    socket.sendMessage(data)
  }
  // 接受服务端返回的数据
  const handleMessage = (data) => {
    console.log('查看返回数据:', data)
    socketData.value = data
  }
  const socketData = ref('')
  let socket = new NewWebSocket('ws://localhost:9000', {}, handleMessage)

  onUnmounted(() => {
    socket.close()
    socket = null
  })
  return {
    startSocket,
    closeSocket,
    socketSend,
    handleMessage,
    socketData
  }
}
export default PushSocket
